package springboot.Controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

import springboot.Controller.dto.UserDTO;
import springboot.common.Constants;
import springboot.common.Result;
import springboot.entity.LjhsGoods;
import springboot.service.ILjhsUserService;
import springboot.entity.LjhsUser;
import org.springframework.web.bind.annotation.RestController;
import springboot.utils.TokenUtils;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hys
 * @since 2022-05-02
 */
@RestController
@RequestMapping("/user")
public class LjhsUserController {
    @Resource
    private ILjhsUserService ljhsUserService;
    @PostMapping("/login")
    public Result Login(@RequestBody UserDTO userDTO, HttpSession session){
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(username)||StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400,"参数错误");
        }

        UserDTO dto = ljhsUserService.login(userDTO);
        session.setAttribute("user_id",dto.getUserId());
        return Result.success(dto);
    }
    @PostMapping("/register")

    public Result Register(@RequestBody UserDTO userDTO){
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(username)||StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400,"参数错误");
        }

        return Result.success(ljhsUserService.register(userDTO));

    }
    @GetMapping("/realname/{realname}")
    public Result findByUsername(@PathVariable String realname) {
        QueryWrapper<LjhsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("realname", realname);
        return Result.success(ljhsUserService.getOne(queryWrapper));
    }
    @PostMapping

    public Result save(@RequestBody LjhsUser ljhsUser) {

        return Result.success(ljhsUserService.saveOrUpdate(ljhsUser));
    }

    @DeleteMapping("/{id}")

    public Result delete(@PathVariable Integer id) {

        return Result.success(ljhsUserService.removeById(id));
    }

    @PostMapping("/batch/del")
    public Result deleteBatch(@RequestBody List<Integer> ids) {

        return Result.success(ljhsUserService.removeByIds(ids));
    }

    @GetMapping

    public Result index() {

        return Result.success(ljhsUserService.list());
    }

    @GetMapping("/{id}")

    public Result findOne(@PathVariable Integer id) {

        return Result.success(ljhsUserService.getById(id));
    }

    @GetMapping("/page")

    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String username) {
        IPage<LjhsUser> page = new Page<>(pageNum, pageSize);
        QueryWrapper<LjhsUser> queryWrapper = new QueryWrapper<>();
        if (!"".equals(username)) {
            queryWrapper.like("username",username);
        }
        queryWrapper.orderByDesc("userId");

        return Result.success(ljhsUserService.page(page,queryWrapper));
    }




}

